/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CopyListUI.java
 *
 * Created on 2010.11.11., 13:10:07
 */

package workshopdesign.UI;

import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.swing.RowFilter;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import workshopdesign.Control.DVDController;
import workshopdesign.Model.Copy;
import workshopdesign.Model.DVD;

/**
 *
 * @author Dom
 */
public class CopyListUI extends javax.swing.JPanel {

    private BaseUIController parent;
    private SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();

    private DVDController dvdController = DVDController.getDVDControl();
    private DefaultTableModel model;
    private TableRowSorter<DefaultTableModel> sorter;

    private Copy selected_copy = null;
    private DVD filter_dvd = null;

    /** Creates new form CopyListUI */
    public CopyListUI(BaseUIController parent, String title) {
        initComponents();

        this.parent = parent;

        this.setBorder(new TitledBorder(title));

        model = (DefaultTableModel) copy_table.getModel();

        sorter = new TableRowSorter<DefaultTableModel>(model);
        copy_table.setRowSorter(sorter);

        filter_field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                filter_data();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                filter_data();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                filter_data();
            }
        });

        load_data();
    }

    public DVD get_filter_dvd() {
        return filter_dvd;
    }

    public void set_filter_dvd(DVD filter_dvd) {
        this.filter_dvd = filter_dvd;
        if(filter_dvd != null){
            sorter.setRowFilter(RowFilter.regexFilter(".*"));
        }
        load_data();
    }

    public void load_data() {
        sdf.applyPattern("yyyy.MM.dd.");
        Collection<Copy> copy_list;
        if(filter_dvd != null){
            copy_list = dvdController.get_Copy_by_DVDId(filter_dvd.getId());
        } else {
            copy_list = dvdController.getCopy();
        }

        model.setRowCount(copy_list.size());

        int row_id = 0;
        for (Copy copy : copy_list) {
            model.setValueAt(copy.getId(), row_id, 0);
            model.setValueAt(sdf.format(copy.getPurchaseDate()), row_id, 1);
            model.setValueAt(copy.getPurchasePrice(), row_id, 2);

            row_id++;
        }
    }

    public void handle_selection() {
        int selected_row = copy_table.getSelectedRow();

        long copy_id = Long.valueOf(String.valueOf(model.getValueAt(selected_row, 0)));
        selected_copy = dvdController.getCopy(copy_id);

        parent.subselection_changed();
    }

    private void filter_data(){
        String filter_text = filter_field.getText();
        if(filter_text.length() != 0){
            set_filter_dvd(null);
        }
        sorter.setRowFilter(RowFilter.regexFilter(".*" + filter_text + ".*"));
    }

    public Copy get_selected_copy() {
        return selected_copy;
    }

    public void clear(){
        selected_copy = null;
        copy_table.clearSelection();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filter_field = new javax.swing.JTextField();
        filter_label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        copy_table = new javax.swing.JTable();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(workshopdesign.WorkshopDesignApp.class).getContext().getResourceMap(CopyListUI.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        filter_field.setText(resourceMap.getString("filter_field.text")); // NOI18N
        filter_field.setName("filter_field"); // NOI18N

        filter_label.setText(resourceMap.getString("filter_label.text")); // NOI18N
        filter_label.setName("filter_label"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        copy_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Purchase date", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Long.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        copy_table.setName("copy_table"); // NOI18N
        copy_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                copy_tableMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(copy_table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(filter_label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(filter_field, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(filter_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(filter_label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void copy_tableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_copy_tableMousePressed
        handle_selection();
    }//GEN-LAST:event_copy_tableMousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable copy_table;
    private javax.swing.JTextField filter_field;
    private javax.swing.JLabel filter_label;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
